package cn.iocoder.yudao.module.ent.controller.admin.company.vo;


import cn.hutool.core.math.MathUtil;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;

@Data
@NoArgsConstructor
public class DetailMenuRespVO {

    private Integer id;

    private String name;

    private String field;

    private Integer totalCount;

    private boolean isTable;

    private List<DetailMenuRespVO> detailMenuVO;


    public static List<DetailMenuRespVO> build(Map<String, Object> queryResult) {
        List<DetailMenuRespVO> result = new ArrayList<>();
        // 基本信息的
        List<DetailMenuRespVO> baseInfoDetailMenu = new ArrayList<>();
        // 基本信息
        baseInfoDetailMenu.add(new DetailMenuRespVO().setId(101).setName("企业信息").setField("basicData").setTotalCount(getValueOrDefault(queryResult, "basicData")).setTable(false));
        baseInfoDetailMenu.add(new DetailMenuRespVO().setId(102).setName("APP").setField("appCount").setTotalCount(getValueOrDefault(queryResult, "appCount")).setTable(true));
        baseInfoDetailMenu.add(new DetailMenuRespVO().setId(103).setName("微信公众号").setField("wechatCount").setTotalCount(getValueOrDefault(queryResult, "wechatCount")).setTable(true));
        baseInfoDetailMenu.add(new DetailMenuRespVO().setId(104).setName("微博").setField("microBlogCount").setTotalCount(getValueOrDefault(queryResult, "microBlogCount")).setTable(true));
        baseInfoDetailMenu.add(new DetailMenuRespVO().setId(105).setName("备案网站").setField("filingWebCount").setTotalCount(getValueOrDefault(queryResult, "filingWebCount")).setTable(true));
        baseInfoDetailMenu.add(new DetailMenuRespVO().setId(106).setName("行政许可").setField("licenseCount").setTotalCount(getValueOrDefault(queryResult, "licenseCount")).setTable(true));
        baseInfoDetailMenu.add(new DetailMenuRespVO().setId(107).setName("进出口信用").setField("importExportCount").setTotalCount(getValueOrDefault(queryResult, "importExportCount")).setTable(true));
        baseInfoDetailMenu.add(new DetailMenuRespVO().setId(108).setName("一般纳税人").setField("taxQualificationCount").setTotalCount(getValueOrDefault(queryResult, "taxQualificationCount")).setTable(true));
        baseInfoDetailMenu.add(new DetailMenuRespVO().setId(109).setName("企业品牌项目").setField("brandCount").setTotalCount(getValueOrDefault(queryResult, "brandCount")).setTable(true));

        DetailMenuRespVO baseInfo = new DetailMenuRespVO().setId(1).setName("基本信息").setField("baseInfo")
                .setTotalCount(baseInfoDetailMenu.stream().mapToInt(DetailMenuRespVO::getTotalCount).reduce(0, Integer::sum))
                .setTable(false)
                .setDetailMenuVO(baseInfoDetailMenu);
        // 工商信息
        List<DetailMenuRespVO> companyDetailMenu = new ArrayList<>();
        companyDetailMenu.add(new DetailMenuRespVO().setId(201).setName("融资信息").setField("financeCount").setTotalCount(getValueOrDefault(queryResult, "financeCount")).setTable(true));
        companyDetailMenu.add(new DetailMenuRespVO().setId(202).setName("股东信息").setField("shareholderInfoCount").setTotalCount(getValueOrDefault(queryResult, "shareholderInfoCount")).setTable(true));
        companyDetailMenu.add(new DetailMenuRespVO().setId(203).setName("主要人员").setField("directors").setTotalCount(getValueOrDefault(queryResult, "directors")).setTable(true));
        companyDetailMenu.add(new DetailMenuRespVO().setId(204).setName("最终受益人").setField("benefitPersonCount").setTotalCount(getValueOrDefault(queryResult, "benefitPersonCount")).setTable(true));
        companyDetailMenu.add(new DetailMenuRespVO().setId(205).setName("对外投资").setField("investmentCount").setTotalCount(getValueOrDefault(queryResult, "investmentCount")).setTable(true));
        companyDetailMenu.add(new DetailMenuRespVO().setId(206).setName("分支机构").setField("branchCount").setTotalCount(getValueOrDefault(queryResult, "branchCount")).setTable(true));
        companyDetailMenu.add(new DetailMenuRespVO().setId(207).setName("控股企业").setField("holdingCompanyCount").setTotalCount(getValueOrDefault(queryResult, "holdingCompanyCount")).setTable(true));
        companyDetailMenu.add(new DetailMenuRespVO().setId(208).setName("变更记录").setField("changeRecordCount").setTotalCount(getValueOrDefault(queryResult, "changeRecordCount")).setTable(true));
        DetailMenuRespVO companyInfo = new DetailMenuRespVO().setId(2).setName("工商信息").setField("companyInfo")
                .setTotalCount(companyDetailMenu.stream().mapToInt(DetailMenuRespVO::getTotalCount).reduce(0, Integer::sum))
                .setTable(false).setDetailMenuVO(companyDetailMenu);
        // 知识产权
        List<DetailMenuRespVO> certDetailMenu = new ArrayList<>();
        certDetailMenu.add(new DetailMenuRespVO().setId(301).setName("商标信息").setField("markCount").setTotalCount(getValueOrDefault(queryResult, "markCount")).setTable(true));
        certDetailMenu.add(new DetailMenuRespVO().setId(302).setName("专利信息").setField("patentCount").setTotalCount(getValueOrDefault(queryResult, "patentCount")).setTable(true));
        certDetailMenu.add(new DetailMenuRespVO().setId(303).setName("软件著作权").setField("copyrightCount").setTotalCount(getValueOrDefault(queryResult, "copyrightCount")).setTable(true));
        certDetailMenu.add(new DetailMenuRespVO().setId(304).setName("作品著作权").setField("workRightCount").setTotalCount(getValueOrDefault(queryResult, "workRightCount")).setTable(true));
        certDetailMenu.add(new DetailMenuRespVO().setId(305).setName("资质证书").setField("qualificationCerCount").setTotalCount(getValueOrDefault(queryResult, "qualificationCerCount")).setTable(true));

        DetailMenuRespVO certInfo = new DetailMenuRespVO().setId(3).setName("知识产权").setField("certInfo")
                .setTotalCount(certDetailMenu.stream().mapToInt(DetailMenuRespVO::getTotalCount).reduce(0, Integer::sum))
                .setTable(false).setDetailMenuVO(certDetailMenu);
        // 招标投标
        List<DetailMenuRespVO> tenderDetailMenu = new ArrayList<>();
        tenderDetailMenu.add(new DetailMenuRespVO().setId(401).setName("招投标信息").setField("tenderBiddingCount").setTotalCount(getValueOrDefault(queryResult, "tenderBiddingCount")).setTable(true));
        DetailMenuRespVO tenderInfo = new DetailMenuRespVO().setId(4).setName("招标投标").setField("tenderInfo")
                .setTotalCount(tenderDetailMenu.stream().mapToInt(DetailMenuRespVO::getTotalCount).reduce(0, Integer::sum))
                .setTable(false).setDetailMenuVO(tenderDetailMenu);
        // 招聘信息
        List<DetailMenuRespVO> jobDetailMenu = new ArrayList<>();
        jobDetailMenu.add(new DetailMenuRespVO().setId(501).setName("招聘信息").setField("enterpriseJobCount").setTotalCount(getValueOrDefault(queryResult, "enterpriseJobCount")).setTable(true));
        DetailMenuRespVO jobInfo = new DetailMenuRespVO().setId(5).setName("招聘信息").setField("jobInfo")
                .setTotalCount(jobDetailMenu.stream().mapToInt(DetailMenuRespVO::getTotalCount).reduce(0, Integer::sum))
                .setTable(false).setDetailMenuVO(jobDetailMenu);
        // 风险信息
        List<DetailMenuRespVO> riskDetailMenu = new ArrayList<>();
        riskDetailMenu.add(new DetailMenuRespVO().setId(601).setName("行政处罚").setField("penaltiesCount").setTotalCount(getValueOrDefault(queryResult, "penaltiesCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(602).setName("经营异常").setField("abnormalCount").setTotalCount(getValueOrDefault(queryResult, "abnormalCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(603).setName("清算组信息").setField("clearAccountCount").setTotalCount(getValueOrDefault(queryResult, "clearAccountCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(604).setName("简易注销公告").setField("simpleCancellationCount").setTotalCount(getValueOrDefault(queryResult, "simpleCancellationCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(605).setName("严重违法").setField("seriousIllegalityCount").setTotalCount(getValueOrDefault(queryResult, "seriousIllegalityCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(606).setName("欠税公告").setField("overdueTaxesCount").setTotalCount(getValueOrDefault(queryResult, "overdueTaxesCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(607).setName("税务非正常户").setField("taxAnomaliesCount").setTotalCount(getValueOrDefault(queryResult, "taxAnomaliesCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(608).setName("税务违法").setField("taxViolationsCount").setTotalCount(getValueOrDefault(queryResult, "taxViolationsCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(609).setName("抽查检查").setField("checkInspectCount").setTotalCount(getValueOrDefault(queryResult, "checkInspectCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(610).setName("立案公告").setField("filingInfoCount").setTotalCount(getValueOrDefault(queryResult, "filingInfoCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(611).setName("开庭公告").setField("openCourtCount").setTotalCount(getValueOrDefault(queryResult, "openCourtCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(612).setName("法院公告").setField("courtNoticedCount").setTotalCount(getValueOrDefault(queryResult, "courtNoticedCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(613).setName("裁判文书").setField("judgmentDocCount").setTotalCount(getValueOrDefault(queryResult, "judgmentDocCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(614).setName("被执行人").setField("executedPersonCount").setTotalCount(getValueOrDefault(queryResult, "executedPersonCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(615).setName("失信被执行人").setField("dishonestExecutorCount").setTotalCount(getValueOrDefault(queryResult, "dishonestExecutorCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(616).setName("限制高消费").setField("restrictConsumptionCount").setTotalCount(getValueOrDefault(queryResult, "restrictConsumptionCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(617).setName("终本案件").setField("finalCaseCount").setTotalCount(getValueOrDefault(queryResult, "finalCaseCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(618).setName("股权出质").setField("equityPledgeCount").setTotalCount(getValueOrDefault(queryResult, "equityPledgeCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(619).setName("知识产权出质").setField("knowledgePledgeCount").setTotalCount(getValueOrDefault(queryResult, "knowledgePledgeCount")).setTable(true));
        riskDetailMenu.add(new DetailMenuRespVO().setId(620).setName("动产抵押").setField("chattelMortgageCount").setTotalCount(getValueOrDefault(queryResult, "chattelMortgageCount")).setTable(true));

        DetailMenuRespVO riskInfo = new DetailMenuRespVO().setId(6).setName("风险信息").setField("riskInfo")
                .setTotalCount(riskDetailMenu.stream().mapToInt(DetailMenuRespVO::getTotalCount).reduce(0, Integer::sum))
                .setTable(false).setDetailMenuVO(riskDetailMenu);

        result.add(baseInfo);
        result.add(companyInfo);
        result.add(certInfo);
        result.add(tenderInfo);
        result.add(jobInfo);
        result.add(riskInfo);


        return result;

    }

    private static Integer getValueOrDefault(Map<String, Object> map, String key) {
        Optional<Object> optionalValue = Optional.ofNullable(map.get(key));
        return optionalValue.filter(obj -> true).map(val -> (Integer) val).orElse(0);
    }
}
